package com.xuan.xwaterfall.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;


/**
 * 环境检测判断
 * 
 * @author ZZJ
 */
public class EnvironmentUtil {

    /**
     * 检查是否插入SD卡
     * 
     * @return
     */
    public static boolean hasSDCard() {
        String status = Environment.getExternalStorageState();
        if (!status.equals(Environment.MEDIA_MOUNTED)) {
            return false;
        }
        return true;
    }

    /**
     * 返回文件保存的根目录
     * 
     * @return
     */
    public static String getRootFilePath() {
        if (hasSDCard()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/
        } else {
            // filePath:/data/data/
            return Environment.getDataDirectory().getAbsolutePath() + "/data/";
        }
    }

    /**
     * 检测当前的网络状态
     * 
     * @param context
     * @return
     */
    public static boolean checkNetState(Context context) {
        boolean netstate = false;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if ((info[i].getState() == NetworkInfo.State.CONNECTED)) {
                        netstate = true;
                        break;
                    }
                }
            }
        }
        return netstate;
    }

    /**
     * 关闭输入面板
     * 
     * @param activity
     */
    public static void closeInputMethod(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),

        InputMethodManager.HIDE_NOT_ALWAYS);
    }


    /**
     * 获取手机IMEI 或 MEID
     * 
     * @param activity Activity
     * @return String DeviceId(GSM手机的 IMEI 和 CDMA手机的 MEID. )
     */
    public static String getPhoneId(Activity activity) {
        TelephonyManager tm = (TelephonyManager) activity.getSystemService(activity.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    public static int getScreenWidth(Activity activity) {
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        return mDisplayMetrics.widthPixels;

    }
}
